<?php

require 'vars.php';
require 'functs.php';
require 'config.install.php';

if ($installed != '1') {
	include 'install.php';
	die;
}
elseif (is_writable('config.install.php') || is_writable('config.mail.php') || is_writable('config.mysql.php')) {
	die ("One of the config files is still writable by the webserver.<br />See README for details.");
}

Authenticate();

require 'config.mysql.php';

header("Cache-Control: no-cache");
Page_startup($title,$version);
Page_headbar($_SESSION[user]);
	
$lastseen = time();

$link = mysql_connect($mysql_server,$mysql_user,$mysql_pass) or die('Unable to connect to MySQL server: ' . mysql_error());
mysql_select_db($mysql_db,$link) or die ('Can\'t use db: ' . mysql_error());

$lastseen = mysql_real_escape_string($lastseen);
$name = mysql_real_escape_string($_SESSION[user]);

$lastseensql = "UPDATE `featherchat_users` SET `lastseen` = '".$lastseen."' WHERE `name` = '".$name."'"  ;
mysql_query($lastseensql) or die ("Unable to update 'lastseen' time in database");
	
?>
<table cellspacing="0">
	<?php
	
	$name = mysql_real_escape_string($_SESSION[user]);
	
	$getcountsql = "SELECT `history` FROM `featherchat_users` WHERE `name` = '".$name."'";
	$result = mysql_query($getcountsql);
	$getcount = mysql_result($result,0) or die ("Unable to retrieve user's history setting.");
	
	$lowestidstring = "SELECT `msgid` FROM `featherchat_messages` ORDER BY `msgid` ASC LIMIT 1";
	$result = mysql_query($lowestidstring);
	$lowestid = @mysql_result($result,0); // Don't error here; a fresh install has 0 posts.
	
	// We need to know the number of actual rows, not just the highest msgid...
	$rowcountsql = 'SELECT `msgid` FROM `featherchat_messages`;';
	$result = mysql_query($rowcountsql);
	$rowcount = mysql_num_rows($result);


	if ($getcount != 101) {	
	
		if ($getcount > $rowcount) { $getcount = $rowcount; }
		$i2 = $getcount;

		if ($rowcount-$i2<0) { $i2 = $lowestid; } // So we don't search for rows that don't exist... ex. LIMIT -4,12
		
			$i=1;
			while ($i <= $getcount) {
	
			$sendstring = "SELECT * FROM `featherchat_messages` ORDER BY `msgid` ASC LIMIT ".($rowcount-$i2).",".$rowcount;
			$result = mysql_query($sendstring) or die ("Unable to retrieve messages!");
			$msg_r = mysql_fetch_assoc($result);

		
			if (!empty($msg_r)) {
				Message_print($msg_r,$_SESSION[user],$i,$getcount);
			}
	
			$i++;
			$i2--;
		} 
	}
	else {
		
		// Let's get a little background information...
		$mymostrecentsql = "SELECT `msgid` FROM `featherchat_messages` WHERE `user` = '".$name."' ORDER BY `msgid` DESC LIMIT 1";
		$result = mysql_query($mymostrecentsql);
		$mymostrecent = mysql_result($result,0);
		
		$mostrecentsql = "SELECT `msgid` FROM `featherchat_messages` ORDER BY `msgid` DESC LIMIT 1";
		$result = mysql_query($mostrecentsql);
		$mostrecent = mysql_result($result,0);
		
		$histminsql = "SELECT `histmin` FROM `featherchat_users` WHERE `name` = '".$name."'";
		$result = mysql_query($histminsql);
		$histmin = mysql_result($result,0);
		
		$rowdiffsql = "SELECT `msgid` FROM `featherchat_messages` WHERE `msgid` >= ".$mymostrecent." AND `msgid` <= ".$mostrecent;
		$result = mysql_query($rowdiffsql);
		$rowdiff = mysql_num_rows($result);
		
		if ($histmin > $rowdiff) { $count = $histmin; } else { $count = $rowdiff; }

		$i=0;
		$i2=$count;

		if ($rowcount-$i2<0) { $i2 = abs($rowcount-$i2)+2; } // So we don't search for rows that don't exist... ex. LIMIT -4,12
		while ($i < $count) {
			$getmessagessql = "SELECT * FROM `featherchat_messages` ORDER BY `msgid` ASC LIMIT ".($rowcount-$i2).",".$rowcount;
			$result = mysql_query($getmessagessql);
			$msg_r = mysql_fetch_assoc($result);

			if (!empty($msg_r)) {
				Message_print($msg_r,$_SESSION[user],$i,$rowdiff+1);
			}
			
			$i++;
			$i2--;
		}
	}

	echo "\n"; ?><tr><td colspan="3">

	<form method="post" action="sent.php">
	<input type="textbox" size="30" name="msg" />
	<input type="submit" value="Send" /> | 	<a href="javascript:location.href='index.php';">Refresh</a>
	</form>
	</td></tr>
</table>
<?php

mysql_close($link);
	
Page_shutdown();
?>